package com.util.DBpool;

import java.sql.*;

/**
 *
 */
public class PoolConnection {
    private Connection conn;
    private boolean isBusy;
    //构造函数
    public PoolConnection(Connection connection, boolean isBusy) {
        this.conn = connection;
        this.isBusy = isBusy;
    }
    public void setConnection(Connection connection) {
        this.conn = connection;
    }
    public Connection getConnection() {
        return this.conn;
    }
    public void setBusy(boolean busy) {
        isBusy = busy;
    }
    //
    public boolean getBusy() {
        return this.isBusy;
    }
    //重写close，将连接放回连接池
    public void close() {
        this.isBusy = false;
    }

    /**
     * 返回执行sql语句后的结果集
     * @param sql
     * @return ResultSet
     */
    public ResultSet query(String sql) {
        PreparedStatement preparedStatement;
        ResultSet resultSet = null;
        try {
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }

    /**
     * 返回PreparedStatement
     * @param sql
     * @return
     * @throws SQLException
     */
    public PreparedStatement getPst(String sql) throws SQLException {
        PreparedStatement preparedStatement = conn.prepareStatement(sql);
        return preparedStatement;
    }

    /**
     * 创建Statement
     */
    public Statement creatStatement() throws SQLException {
        return conn.createStatement();
    }

    /**
     * 获取数据库元数据
     * @return DataBaseMetaData
     * @throws SQLException
     */
    public DatabaseMetaData getMetaData() throws SQLException {
        return conn.getMetaData();
    }

}
